﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CUREEntity;
using DBUtility;

namespace CUREDataAccessLayers
{
    public class LoginDAL
    {
        public string GetPassword(string str)
        {
            string result = "";
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select PASSWORD from master t where trim(NAME)='" + str + "'");
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    result = obj.ToString();
                }
                //result = this.DbHelperInstance.ExecuteScalar<string>(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 查找ID
        /// </summary>
        /// <param name="s1">用户名</param>
        /// <param name="s2">密码</param>
        /// <returns>string类型的MASTER_ID</returns>
        public Decimal GetID(string s1, string s2)
        {
            Decimal result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select ID from  master where NAME='" + s1 + "' and PASSWORD='" + s2 + "'");
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    result = Convert.ToDecimal(obj);
                }
                //result = this.DbHelperInstance.ExecuteScalar<Decimal>(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public Decimal GetID(string s1)
        {
            Decimal result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select ID from  master where NAME='" + s1 + "'");
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    result = Convert.ToDecimal(obj);
                }
                //result = this.DbHelperInstance.ExecuteScalar<Decimal>(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 查询GROUP_ID
        /// </summary>
        /// <param name="s1">用户名</param>
        /// <param name="s2">用户密码</param>
        /// <returns></returns>
        public Decimal GetGROUP_ID(string name, string password)
        {
            Decimal result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select GROUP_ID from mastergroup  a join master  b on a.MASTER_ID=b.ID where trim(b.NAME)='" + name + "' and trim(b.PASSWORD)='" + password + "'");
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    result = Convert.ToDecimal(obj);
                }
                //result = this.DbHelperInstance.ExecuteScalar<Decimal>(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public Decimal GetGROUP_ID(string name)
        {
            Decimal result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select GROUP_ID from mastergroup  a join master  b on a.MASTER_ID=b.ID where trim(b.NAME)='" + name + "'");
                Object obj = DbHelperOra.GetSingle(commandText.ToString());
                if (!Object.Equals(obj, null))
                {
                    result = Convert.ToDecimal(obj);
                }
                //result = this.DbHelperInstance.ExecuteScalar<Decimal>(commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据角色判断此用户是否有权限
        /// </summary>
        /// <param name="where">groupid:角色，url：路径</param>
        /// <returns></returns>
        public bool JudgeAuthorityByGroupID(IDictionary<string, object> where)
        {
            bool judge = false;
            string groupid = string.Empty;
            string url = string.Empty;
            DataTable dt = new DataTable();
            #region 处理参数
            if (where.ContainsKey("groupid") && where["groupid"] != null && where["groupid"].ToString() != "")
            {
                groupid = where["groupid"].ToString().Trim();
            }
            if (where.ContainsKey("url") && where["url"] != null && where["url"].ToString() != "")
            {
                url = where["url"].ToString().Trim();
            }
            #endregion
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from actiongroup t where t.group_id='" + groupid + "' and t.action='" + url + "'");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                // this.DbHelperInstance.Fill(dt, sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    judge = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return judge;
        }
        /// <summary>
        /// 生成登录日志
        /// </summary>
        public bool WriteOnline(string UserId, string UserName, string HostName)
        {
            bool lb_return = false;
            string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            StringBuilder commandText = new StringBuilder();
            commandText.Append(" insert into sys_online(userid,username,lastlogtime,hostname) values(");
            commandText.Append(UserId + ",'" + UserName + "',to_date('" + Now + "','yyyy-mm-dd hh24:mi:ss'),'");
            commandText.Append(HostName + "')");

            try
            {
                int i = 0;
                i = DbHelperOra.ExecuteSql(commandText.ToString());
                if (i > 0)
                { lb_return = true; }
                else
                { lb_return = false; }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lb_return;
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        public bool DeleteOnline(string UserId, string HostName)
        {
            bool lb_return = false;
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" delete from sys_online where userid=" + UserId + " and hostname='" + HostName + "'");

            try
            {
                int i = 0;
                i = DbHelperOra.ExecuteSql(commandText.ToString());
                if (i > 0)
                { lb_return = true; }
                else
                { lb_return = false; }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lb_return;
        }
        /// <summary>
        /// 查询登录
        /// </summary>
        public bool IsOnline(string UserId, string HostName)
        {
            bool lb_return = false;

            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select * from sys_online where userid=" + UserId + " and hostname='" + HostName + "'");
            try
            {
                dt = DbHelperOra.QueryTable(commandText.ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    lb_return = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lb_return;
        }


    }
}
